package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomAttrValueService attrValueService;
    @Autowired
    private RoomAttrValueMapper attrValueMapper;
    @Autowired
    private RoomFacilityService facilityService;
    @Autowired
    private RoomFacilityMapper facilityMapper;
    @Autowired
    private RoomLabelService labelService;
    @Autowired
    private RoomLabelMapper labelMapper;
    @Autowired
    private RoomPaymentTypeService paymentTypeService;
    @Autowired
    private RoomPaymentTypeMapper paymentTypeMapper;
    @Autowired
    private RoomLeaseTermService leaseTermService;
    @Autowired
    private RoomLeaseTermMapper leaseTermMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Override
    public IPage<RoomItemVo> pageRoomItemVo(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemVo(page, queryVo);
    }

    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomDetailVo detail = new RoomDetailVo();
        RoomInfo one = this.getOne(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getId, id));
        ApartmentInfo apartmentInfo = apartmentInfoService.getOne(new LambdaQueryWrapper<ApartmentInfo>().eq(ApartmentInfo::getId, one.getApartmentId()));
        List<GraphVo> graphVos = graphInfoMapper.getGraphById(id);
        List<AttrValueVo> valueVoList = attrValueMapper.getAttrValueVoList(id);
        List<FacilityInfo> facilityList = facilityMapper.getFacilityList(id);
        List<LabelInfo> labelList = labelMapper.getLabelList(id);
        List<PaymentType> paymentTypeList = paymentTypeMapper.getPaymentTypeList(id);
        List<LeaseTerm> leaseTermList = leaseTermMapper.getLeaseTermList(id);
        BeanUtils.copyProperties(one, detail);
        detail.setGraphVoList(graphVos);
        detail.setApartmentInfo(apartmentInfo);
        detail.setAttrValueVoList(valueVoList);
        detail.setFacilityInfoList(facilityList);
        detail.setLabelInfoList(labelList);
        detail.setPaymentTypeList(paymentTypeList);
        detail.setLeaseTermList(leaseTermList);
        return detail;
    }

    @Override
    public void saveOrUpdateroomSubmitVo(RoomSubmitVo roomSubmitVo) {
        Long id = roomSubmitVo.getId();

        this.saveOrUpdate(roomSubmitVo);
        if (id != null) {
            attrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
            facilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
            labelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
            paymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
            leaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
            graphInfoService.remove(new LambdaUpdateWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));
        }
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<GraphVo> graphInfoList = roomSubmitVo.getGraphVoList();

        List<RoomAttrValue> roomAttrValueList = new ArrayList<>();
        List<RoomFacility> roomFacilityList = new ArrayList<>();
        List<RoomLabel> roomLabelList = new ArrayList<>();
        List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
        List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
        List<GraphInfo> graphInfoList1 = new ArrayList<>();
        attrValueIds.forEach(attr -> {
            RoomAttrValue attrValue = RoomAttrValue.builder().roomId(roomSubmitVo.getId()).attrValueId(attr).build();
            roomAttrValueList.add(attrValue);
        });
        facilityInfoIds.forEach(fac -> {
            RoomFacility facility = RoomFacility.builder().roomId(roomSubmitVo.getId()).facilityId(fac).build();
            roomFacilityList.add(facility);
        });
        labelInfoIds.forEach(label -> {
            RoomLabel labelInfo = RoomLabel.builder().roomId(roomSubmitVo.getId()).labelId(label).build();
            roomLabelList.add(labelInfo);
        });
        paymentTypeIds.forEach(payment -> {
            RoomPaymentType paymentType = RoomPaymentType.builder().roomId(roomSubmitVo.getId()).paymentTypeId(payment).build();
            roomPaymentTypeList.add(paymentType);
        });
        leaseTermIds.forEach(lease -> {
            RoomLeaseTerm leaseTerm = RoomLeaseTerm.builder().roomId(roomSubmitVo.getId()).leaseTermId(lease).build();
            roomLeaseTermList.add(leaseTerm);
        });
        graphInfoList.forEach(graph -> {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graph.getName());
            graphInfo.setUrl(graph.getUrl());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfoList1.add(graphInfo);
        });
        attrValueService.saveBatch(roomAttrValueList);
        facilityService.saveBatch(roomFacilityList);
        labelService.saveBatch(roomLabelList);
        paymentTypeService.saveBatch(roomPaymentTypeList);
        leaseTermService.saveBatch(roomLeaseTermList);
        graphInfoService.saveBatch(graphInfoList1);

    }

    @Override
    public void removeRoomInfoById(Long id) {
        this.remove(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getId, id));
        attrValueService.remove(new LambdaQueryWrapper<RoomAttrValue>().eq(RoomAttrValue::getRoomId, id));
        facilityService.remove(new LambdaQueryWrapper<RoomFacility>().eq(RoomFacility::getRoomId, id));
        labelService.remove(new LambdaQueryWrapper<RoomLabel>().eq(RoomLabel::getRoomId, id));
        paymentTypeService.remove(new LambdaQueryWrapper<RoomPaymentType>().eq(RoomPaymentType::getRoomId, id));
        leaseTermService.remove(new LambdaQueryWrapper<RoomLeaseTerm>().eq(RoomLeaseTerm::getRoomId, id));
        graphInfoService.remove(new LambdaUpdateWrapper<GraphInfo>().eq(GraphInfo::getItemId, id).eq(GraphInfo::getItemType, ItemType.ROOM));

}
}




